/* Copyright 2010 Hugh Greenberg */  

package com.hugegreenbug.msoss;

import java.nio.ByteBuffer;

public class Background {
	private volatile int mScaledWidth;
	private volatile int mScaledHeight;
	private volatile ByteBuffer mBuffer;
	private volatile int mTexWidth;
	private volatile int mTexHeight;
	private volatile float mZoomScale;
	private volatile float mStretchScale;
	private volatile float mZoomWidthDiff;
	private volatile float mZoomHeightDiff;
	private volatile float mCubeScale;
	private volatile float mCubeTrans;
	
	public Background() {
		mScaledWidth = 0;
		mScaledHeight = 0;
		mTexWidth = 0;
		mTexHeight = 0;
		mBuffer = null;
		mZoomScale = 1.0f;
		mStretchScale = 1.0f;
		mZoomWidthDiff = 0.0f;
		mZoomHeightDiff = 0.0f;
		mCubeScale = 1.0f;
		mCubeTrans = -5.0f;
	}
	
	public void setByteBuffer(ByteBuffer buf) {
		mBuffer = buf;
	}
	
	public ByteBuffer getByteBuffer() {
		return mBuffer;
	}
	
	public void setScaledWidth(int width) {
		mScaledWidth = width;
	}
	
	public void setScaledHeight(int height) {
		mScaledHeight = height;
	}
	
	public int getScaledWidth() {
		return mScaledWidth;
	}
	
	public int getScaledHeight() {
		return mScaledHeight;
	}
	
	public void setTexWidth(int width) {
		mTexWidth = width;
	}
	
	public void setTexHeight(int height) {
		mTexHeight = height;
	}
	
	public int getTexWidth() {
		return mTexWidth;
	}
	
	public int getTexHeight() {
		return mTexHeight;
	}
	
	public void setZoomScale(float scale) {
		mZoomScale = scale;
	}
	
	public void setStretchScale(float scale) {
		mStretchScale = scale;
	}
	
	public void setZoomWidthDiff(float diff) {
		mZoomWidthDiff = diff;
	}
	
	public void setZoomHeightDiff(float diff) {
		mZoomHeightDiff = diff;
	}
	
	public float getZoomScale() {
		return mZoomScale;
	}
	
	public float getStretchScale() {
		return mStretchScale;
	}
	
	public float getZoomWidthDiff() {
		return mZoomWidthDiff;
	}
	
	public float getZoomHeightDiff() {
		return mZoomHeightDiff;
	}
	
	public float getCubeScale() {
		return mCubeScale;
	}
	
	public void setCubeScale(float scale) {
		mCubeScale = scale;
	}
	
	public void setCubeTrans(float trans) {
		mCubeTrans = trans;
	}
	
	public float getCubeTrans() {
		return mCubeTrans;
	}
	
	public void setScales(int width, int height) {
		float widthDiff = 0.0f;
 		float heightDiff = 0.0f;
 		float nscale = 1.0f;
 		float sscale = 1.0f;
 		float cscale = 1.0f;
 		float ctrans = -5.0f;
     	try {
			float nscalew = (float) width/mScaledWidth;
			float nscaleh = (float) height/mScaledHeight;
			nscale = Math.max(nscalew, nscaleh);
			sscale = nscale;
			cscale = 1.0f;
			float ratio = (float) width/height;
			if (ratio < 1.0f)
				ctrans = -6.2f;
			else
				ctrans = -4.0f;
			
			if (nscale < 1.5f) {
				nscale *= 2.0f;
			}
		
			float scaledWidth = mScaledWidth * nscale;
			float scaledHeight = mScaledHeight * nscale;
			nscalew = (float) mScaledWidth/mScaledHeight;
			nscaleh = (float) mScaledHeight/mScaledWidth;
			float wdiff = scaledWidth - width;
			float hdiff = scaledHeight - height;
			widthDiff = wdiff/width;
			heightDiff = hdiff/height;
		} catch (ArithmeticException ae) {
			sscale = 1.0f;
			widthDiff = 0.0f;
			heightDiff = 0.0f;
		}
	
		setZoomScale(nscale);
		setStretchScale(sscale);
		setZoomWidthDiff(widthDiff);
		setZoomHeightDiff(heightDiff);
		setCubeScale(cscale); 
		setCubeTrans(ctrans);
	}
}
